package com.edmond.controller;

//import com.edmond.entity.Menu;
//import com.edmond.feign.MenuFeign;

import com.edmond.entity.Menu;
import com.edmond.entity.MenuVO;
import com.edmond.entity.Type;
import com.edmond.feign.MenuFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
@RequestMapping("/menu")
@EnableAutoConfiguration
public class MenuHandler {
    @Autowired
    private MenuFeign menuFeign;

    @GetMapping("/findAll")
    @ResponseBody
    public MenuVO findAll(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        return menuFeign.findAll((page - 1) * limit, limit);
    }

    //  测试layui 是否引入的页面跳转功能
    @GetMapping("/redirect/{location}")
    public String redirect(@PathVariable("location") String location) {
        return location;
    }

    //  根据id删除菜品
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") long id) {
        menuFeign.deleteById(id);
        return "redirect:/menu/redirect/menu_manage";
    }

    //  查询所有菜品类别信息，并跳转到添加页面
    @GetMapping("/findTypes")
    public ModelAndView findTypes() {
        ModelAndView modelAndView = new ModelAndView();
        //  设置返回视图的名称 ，与配置文件中的内容 拼接，访问的是 ：/static/menu_add.html
        modelAndView.setViewName("menu_add");
        modelAndView.addObject("list", menuFeign.findTypes());
        return modelAndView;
    }

    //  新增菜品
    @PostMapping("/save")
    public String save(Menu menu) {
        menuFeign.save(menu);
        return "redirect:/menu/redirect/menu_manage";
    }

    //  修改菜品，查询菜品信息和菜品类别信息，将信息封装到model对象中
    //  跳转到修改页面
    @GetMapping("/findById/{id}")
    public ModelAndView findById(@PathVariable("id") long id) {
        ModelAndView modelAndView = new ModelAndView();
        Menu menu = menuFeign.findById(id);
        List<Type> list = menuFeign.findTypes();
        //  设置视图名称
        modelAndView.setViewName("menu_update");
        //  向对应视图中添加元素
        modelAndView.addObject("list", list);
        modelAndView.addObject("menu", menu);
        return modelAndView;
    }

    //  更新菜品信息
    @PostMapping("/update")
    public String update(Menu menu){
        menuFeign.update(menu);
        return "redirect:/menu/redirect/menu_manage";
    }
}
